﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Ascent.QiPei.Module.BusinessObjects.Enums;
using Ascent.QiPei.Module.BusinessObjects.Finance;
using Ascent.QiPei.Module.BusinessObjects.Sales;
using Ascent.QiPei.Module.BusinessObjects.Common;
using DevExpress.Data.Filtering;
using DevExpress.ExpressApp;
using DevExpress.XtraEditors;
using DevExpress.Xpo;
using Ascent.QiPei.Module.BusinessObjects.Base;

namespace Ascent.QiPei.Module.Win.Sales
{
    public partial class BalanceForm : DevExpress.XtraEditors.XtraForm
    {
        private SaleOrder saleOrder;
        IObjectSpace objectSpace;
        Session mainSession;
        public BalanceForm()
        {
            InitializeComponent();
        }

        public BalanceForm(IObjectSpace os, Session session, SaleOrder so)
        {
            InitializeComponent();
            this.objectSpace = os;
            this.saleOrder = so;
            this.mainSession = session;
            ReCountAmount();
            LoadBalanceData();
        }

        private void LoadBalanceData()
        {
            this.txtTotalAmount.Text = this.saleOrder.TotalAmount.ToString();
            this.txtDiscount.Text = this.saleOrder.Discount.ToString();
            this.txtDiscountAmount.Text = this.saleOrder.DiscountAmount.ToString();
            this.txtTaxRate.Text = this.saleOrder.TaxRate.ToString();
            this.txtTaxAmount.Text = this.saleOrder.TaxAmount.ToString();
            this.txtAmount.Text = this.saleOrder.Amount.ToString();

            this.txtReceivedCash.Text = this.saleOrder.ReceivedCash.ToString();
            this.txtReceivedRemitCash.Text = this.saleOrder.ReceivedRemitCash.ToString();
            this.txtReceivedUnpaid.Text = this.saleOrder.ReceivedUnpaid.ToString();
            this.txtReceivedCollection.Text = this.saleOrder.ReceivedCollection.ToString();
            this.txtGargoServiceCharge.Text = this.saleOrder.GargoServiceCharge.ToString();
            this.txtGargoFare.Text = this.saleOrder.GargoFare.ToString();
            this.txtGargoTeminal.Text = this.saleOrder.GargoTeminal.GargoName;
            this.dtTimeOfPayment.Text = this.saleOrder.TimeOfPayment.ToString();
            this.txtRemarks.Text = this.saleOrder.Remarks.ToString();
        }

        private void ReCountAmount()
        {
            this.saleOrder.DiscountAmount = this.saleOrder.TotalAmount - this.saleOrder.TotalAmount * this.saleOrder.Discount / 100;
            this.saleOrder.TaxAmount = this.saleOrder.DiscountAmount * this.saleOrder.TaxRate / 100;
            this.saleOrder.Amount = this.saleOrder.DiscountAmount + this.saleOrder.TaxAmount;
        }

        private void TextBoxAmount()
        {
            this.txtTotalAmount.Text = this.saleOrder.TotalAmount.ToString();
            this.txtDiscount.Text = this.saleOrder.Discount.ToString();
            this.txtDiscountAmount.Text = this.saleOrder.DiscountAmount.ToString();
            this.txtTaxRate.Text = this.saleOrder.TaxRate.ToString();
            this.txtTaxAmount.Text = this.saleOrder.TaxAmount.ToString();
            this.txtAmount.Text = this.saleOrder.Amount.ToString();
        }

        private void Save()
        {
            saleOrder.SoStatus = OrderStatusType.已确认;
            CodeUtil codeUtil = new CodeUtil(mainSession);
            saleOrder.Discount = short.Parse(this.txtDiscount.Value.ToString());
            saleOrder.DiscountAmount = float.Parse(this.txtDiscountAmount.Text);
            saleOrder.TaxRate = short.Parse(this.txtTaxRate.Value.ToString());
            saleOrder.TaxAmount = float.Parse(this.txtTaxAmount.Value.ToString());
            saleOrder.Amount = float.Parse(this.txtAmount.Value.ToString());


            saleOrder.ReceivedCash = float.Parse(this.txtReceivedCash.Value.ToString());
            saleOrder.ReceivedRemitCash = float.Parse(this.txtReceivedRemitCash.Value.ToString());
            saleOrder.ReceivedCollection = float.Parse(this.txtReceivedCollection.Value.ToString());
            saleOrder.ReceivedUnpaid = float.Parse(this.txtReceivedUnpaid.Value.ToString());

            saleOrder.GargoServiceCharge = float.Parse(this.txtGargoServiceCharge.Value.ToString());
            saleOrder.GargoFare = float.Parse(this.txtGargoFare.Value.ToString());
            saleOrder.GargoTeminal = objectSpace.FindObject<GargoTeminal>(CriteriaOperator.Parse("GargoName = ?", this.txtGargoTeminal.Text));
            saleOrder.TimeOfPayment = this.dtTimeOfPayment.DateTime;
            saleOrder.Remarks = this.txtRemarks.Text;

            #region 更新现金帐--付现金
            if (saleOrder.ReceivedCash > 0)
            {
                CashAccount cashAccount = objectSpace.CreateObject<CashAccount>();
                cashAccount.CATCode = codeUtil.GenerateCode(CodeNameType.现金帐);
                cashAccount.IncomeAmount = saleOrder.ReceivedCash;
                cashAccount.PayAmount = 0;
                cashAccount.CustomOrSupplier = saleOrder.Customer.CustomerCode;
                cashAccount.Summary = "现金";
                cashAccount.Date = DateTime.Now;
                cashAccount.AssociationNubmers = saleOrder.SOCode;
                cashAccount.Remark = string.Empty;
            }
            #endregion

            #region 更新现金帐--汇款
            if (saleOrder.ReceivedRemitCash > 0)
            {
                CashAccount cashAccount = objectSpace.CreateObject<CashAccount>();
                cashAccount.CATCode = codeUtil.GenerateCode(CodeNameType.现金帐);
                cashAccount.IncomeAmount = saleOrder.ReceivedRemitCash;
                cashAccount.PayAmount = 0;
                cashAccount.CustomOrSupplier = saleOrder.Customer.CustomerCode;
                cashAccount.Summary = "汇款";
                cashAccount.Date = DateTime.Now;
                cashAccount.AssociationNubmers = saleOrder.SOCode;
                cashAccount.Remark = string.Empty;
            }
            #endregion

            #region 更新现金帐--运费
            if (saleOrder.GargoFare > 0)
            {
                CashAccount cashAccount = objectSpace.CreateObject<CashAccount>();
                cashAccount.CATCode = codeUtil.GenerateCode(CodeNameType.现金帐);
                cashAccount.IncomeAmount = 0;
                cashAccount.PayAmount = saleOrder.GargoFare;
                cashAccount.CustomOrSupplier = saleOrder.Customer.CustomerCode;
                cashAccount.Summary = "运费";
                cashAccount.Date = DateTime.Now;
                cashAccount.AssociationNubmers = saleOrder.SOCode;
                cashAccount.Remark = string.Empty;
            }
            #endregion

            #region 更新应收帐--挂帐
            if (saleOrder.ReceivedUnpaid > 0)
            {
                AccountReceivable accReceivable = objectSpace.FindObject<AccountReceivable>(CriteriaOperator.Parse("Customer.CustomerCode = ?", saleOrder.Customer.CustomerCode));
                if (accReceivable == null)
                {
                    accReceivable = objectSpace.CreateObject<AccountReceivable>();
                    accReceivable.ARCCode = codeUtil.GenerateCode(CodeNameType.应收帐);
                    accReceivable.Customer = saleOrder.Customer;
                    accReceivable.CustomerTelphone = saleOrder.Customer.Telphone;
                    accReceivable.ReceivableAmount = saleOrder.ReceivedUnpaid;
                    accReceivable.ReceivabledAmount = 0;
                    accReceivable.DebtAmount = accReceivable.ReceivableAmount;
                    accReceivable.Remark = string.Empty;
                }
                else
                {
                    accReceivable.ReceivableAmount += saleOrder.ReceivedUnpaid;
                    accReceivable.DebtAmount += saleOrder.ReceivedUnpaid;
                    accReceivable.Remark = string.Empty;
                }
                //AccountReceivableItems accReceivableItem = objectSpace.CreateObject<AccountReceivableItems>();
                //accReceivableItem.AccountReceivable = accReceivable;
                //accReceivableItem.ReceivableDate = DateTime.Now;
                //accReceivableItem.ModeOfPayment = ModeOfPaymentType.挂帐;
                //accReceivableItem.CurrentAmount = saleOrder.ReceivedUnpaid;
                //accReceivableItem.AssociationNubmers = saleOrder.SOCode;
                //accReceivableItem.Summary = "挂帐";
                //accReceivableItem.Remark = string.Empty;
            }
            #endregion

            #region 更新应收帐--代收
            if (saleOrder.ReceivedCollection > 0)
            {
                AccountReceivable accReceivable = objectSpace.FindObject<AccountReceivable>(CriteriaOperator.Parse("Customer.CustomerCode = ?", saleOrder.Customer.CustomerCode));
                if (accReceivable == null)
                {
                    accReceivable = objectSpace.CreateObject<AccountReceivable>();
                    accReceivable.ARCCode = codeUtil.GenerateCode(CodeNameType.应收帐);
                    accReceivable.Customer = saleOrder.Customer;
                    accReceivable.CustomerTelphone = saleOrder.Customer.Telphone;
                    accReceivable.ReceivableAmount = saleOrder.ReceivedCollection;
                    accReceivable.ReceivabledAmount = 0;
                    accReceivable.DebtAmount = saleOrder.ReceivedCollection;
                    accReceivable.Remark = string.Empty;
                }
                else
                {
                    accReceivable.ReceivableAmount += saleOrder.ReceivedCollection;
                    accReceivable.DebtAmount += saleOrder.ReceivedCollection;
                    accReceivable.Remark = string.Empty;
                }
                //AccountReceivableItems accReceivableItem = objectSpace.CreateObject<AccountReceivableItems>();
                //accReceivableItem.AccountReceivable = accReceivable;
                //accReceivableItem.ReceivableDate = DateTime.Now;
                //accReceivableItem.ModeOfPayment = ModeOfPaymentType.代收;
                //accReceivableItem.CurrentAmount = saleOrder.ReceivedCollection;
                //accReceivableItem.AssociationNubmers = saleOrder.SOCode;
                //accReceivableItem.Summary = "代收";
                //accReceivableItem.Remark = string.Empty;
            }
            #endregion

            #region 更新应付帐--手续费
            if (saleOrder.GargoServiceCharge > 0)
            {
                AccountPayable accPayable = objectSpace.FindObject<AccountPayable>(CriteriaOperator.Parse("GargoTeminal.GargoName = ?", saleOrder.GargoTeminal.GargoName));
                if (accPayable == null)
                {
                    accPayable = objectSpace.CreateObject<AccountPayable>();
                    accPayable.APYCode = codeUtil.GenerateCode(CodeNameType.应付帐);
                    accPayable.GargoTeminal = saleOrder.GargoTeminal;
                    accPayable.GargoTeminalTelephone = saleOrder.GargoTeminal.Phone;
                    accPayable.PayableAmount = saleOrder.GargoServiceCharge;
                    accPayable.Remark = string.Empty;
                }
                else
                {
                    accPayable.PayableAmount += saleOrder.GargoServiceCharge;
                    accPayable.DebtAmount += saleOrder.GargoServiceCharge;
                    accPayable.Remark = string.Empty;
                }
                //AccountPayableItems accPayItem = objectSpace.CreateObject<AccountPayableItems>();
                //accPayItem.AccountPayable = accPayable;
                //accPayItem.PayableDate = DateTime.Now;
                //accPayItem.ModeOfPayment = ModeOfPaymentType.手续费;
                //accPayItem.CurrentAmount = saleOrder.GargoServiceCharge;
                //accPayItem.AssociationNubmers = saleOrder.SOCode;
                //accPayItem.Summary = "手续费";
                //accPayItem.Remark = string.Empty;
            }
            #endregion

            objectSpace.CommitChanges();

        }

        private void btnSavePrint_Click(object sender, EventArgs e)
        {
            this.Save();
            XtraMessageBox.Show("保存成功", "提示", MessageBoxButtons.OK);
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

        }

        private void txtDiscount_EditValueChanged(object sender, EventArgs e)
        {
            this.saleOrder.Discount = short.Parse(this.txtDiscount.Value.ToString());
            ReCountAmount();
            LoadBalanceData();
        }

        private void txtTaxRate_EditValueChanged(object sender, EventArgs e)
        {
            this.saleOrder.TaxRate = short.Parse(this.txtTaxRate.Value.ToString());
            ReCountAmount();
            LoadBalanceData();
        }

        private void txtTaxAmount_EditValueChanged(object sender, EventArgs e)
        {
            this.saleOrder.TaxAmount = float.Parse(this.txtTaxAmount.Text);
            this.saleOrder.Amount = this.saleOrder.DiscountAmount + this.saleOrder.TaxAmount;
            this.txtAmount.Text = this.saleOrder.Amount.ToString();
        }

        private void txtAmount_EditValueChanged(object sender, EventArgs e)
        {
            this.saleOrder.Amount = float.Parse(this.txtAmount.Text);
            this.saleOrder.ReceivedCash = this.saleOrder.Amount;
            this.saleOrder.ReceivedRemitCash = 0;
            this.saleOrder.ReceivedUnpaid = 0;
            this.saleOrder.ReceivedCollection = 0;
            this.saleOrder.GargoServiceCharge = 0;
            this.saleOrder.GargoFare = 0;
            LoadBalanceData();
        }

        private void ReCountPay()
        {
            this.saleOrder.ReceivedUnpaid = this.saleOrder.Amount - this.saleOrder.ReceivedCash - this.saleOrder.ReceivedRemitCash - this.saleOrder.ReceivedCollection;
            if (this.saleOrder.ReceivedUnpaid < 0)
                this.saleOrder.ReceivedUnpaid = 0;
        }

        private void TextBoxPay()
        {
            this.txtReceivedCash.Text = this.saleOrder.ReceivedCash.ToString();
            this.txtReceivedRemitCash.Text = this.saleOrder.ReceivedRemitCash.ToString();
            this.txtReceivedUnpaid.Text = this.saleOrder.ReceivedUnpaid.ToString();
            this.txtReceivedCollection.Text = this.saleOrder.ReceivedCollection.ToString();
            this.txtGargoServiceCharge.Text = this.saleOrder.GargoServiceCharge.ToString();
            this.txtGargoFare.Text = this.saleOrder.GargoFare.ToString();
        }

        private void txtReceivedCash_EditValueChanged(object sender, EventArgs e)
        {
            this.saleOrder.ReceivedCash = float.Parse(this.txtReceivedCash.Text);
            ReCountPay();
            LoadBalanceData();
        }

        private void txtReceivedRemitCash_EditValueChanged(object sender, EventArgs e)
        {
            this.saleOrder.ReceivedRemitCash = float.Parse(this.txtReceivedRemitCash.Text);
            ReCountPay();
            LoadBalanceData();
        }

        private void txtReceivedCollection_EditValueChanged(object sender, EventArgs e)
        {
            this.saleOrder.ReceivedCollection = float.Parse(this.txtReceivedCollection.Text);
            ReCountPay();
            LoadBalanceData();
        }
    }
}